package com.white.controller;

import com.white.bean.Communicate;
import com.white.service.CommunicateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CommunicateController {
    @Autowired
    CommunicateService communicateService;

    /**
     * 获取聊天记录
     * @param responseUserId 响应用户id
     * @param loginUserId 登录用户id
     * @return 聊天数据
     */
    @GetMapping("/getCommunicates/{responseUserId}")
    @ResponseBody
    public List<Communicate> getCommunicates(@PathVariable("responseUserId") Long responseUserId,
                                             @CookieValue(UserController.LOGIN_USER_ID) Long loginUserId) {
        return communicateService.search(loginUserId, responseUserId);
    }

    /**
     * 保存交流记录
     * @param responseUserId
     * @return
     */
    @PostMapping("/saveCommunicate/{responseUserId}")
    @ResponseBody
    public void saveCommunicate(@PathVariable("responseUserId") Long responseUserId,
                                @RequestParam("content") String content,
                                @CookieValue(UserController.LOGIN_USER_ID) Long loginUserId) {
        Communicate communicate = new Communicate(loginUserId, responseUserId, content);

        try {
            communicateService.save(communicate);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
